﻿using Artech.Common;
using Microsoft.Practices.Unity.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Artech.Core
{
    /// <summary>
    /// The class represents a single context item.
    /// </summary>
    [DataContract(Namespace = Constants.Namespaces.DataContract)]
    public class ContextItem: ICloneable
    {
        private object value;

        /// <summary>
        /// Gets the key.
        /// </summary>
        /// <value>
        /// The key.
        /// </value>
        [DataMember]
        public string Key { get; private set; }

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>
        /// The value.
        /// </value>
        [DataMember]
        public object Value
        {
            get { return value; }
            set 
            {
                Guard2.IsStringOrValueType(value, "value");
                this.value = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is local.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is local; otherwise, <c>false</c>.
        /// </value>
        public bool IsLocal { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="ContextItem"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public ContextItem(string key, object value)
        {
            Guard.ArgumentNotNullOrEmpty(key, "key");
            Guard2.IsStringOrValueType(value, "value");
            this.Key = key;
            this.Value = value;
        }

        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        object ICloneable.Clone()
        {
            return this.Clone();
        }

        /// <summary>
        /// Clones this instance.
        /// </summary>
        /// <returns></returns>
        public ContextItem Clone()
        {
            return new ContextItem(this.Key, this.value) { IsLocal = this.IsLocal };
        }
    }
}